package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询参数为：{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable("status") Integer status, Long id) {

        categoryService.startOrStop(status, id);
        return Result.success("修改成功");
    }

    @PostMapping
    public Result<String> save(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增菜品分类:{}", categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success("添加成功");
    }

    @PutMapping
    public Result<String> update(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改菜品分类信息:{}", categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success("修改成功");
    }

    @DeleteMapping
    public Result<String> delete( Long id) {
        log.info("要删除菜品分类:{}",id);
        categoryService.deleteById(id);
        return Result.success("删除成功");
    }
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type){
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }
}
